package com.example.log.controller;

import com.example.log.entity.User;
import com.example.log.myaop.OperationType;
import com.example.log.myaop.UserLog;
import com.example.log.service.IUserService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    private final IUserService userService;

    public UserController(IUserService userService) {
        this.userService = userService;
    }

    @PostMapping("/add")
    @UserLog(type = OperationType.INSERT)
    public void add(@RequestBody User user) {
        userService.save(user);
    }

    @GetMapping("/select")
    @UserLog(type = OperationType.SELECT)
    public User selectById(@RequestParam("id") String id, @RequestParam("ids") String ids) {
        System.out.println("id=" + id);
        return userService.lambdaQuery()
                .eq(User::getId, id)
                .one();
    }

    @PostMapping("/update")
    @UserLog(type = OperationType.UPDATE)
    public void update(@RequestBody User user) {
        userService.updateById(user);
    }

    @DeleteMapping("/delete")
    @UserLog(type = OperationType.DELETE)
    public void delete(@RequestParam("id") String id) {
        userService.removeById(id);
    }
}
